package cn.gailvlun.gll.presentation.chat.vm

import android.arch.lifecycle.ViewModel
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.net.apartment.room.Room
import io.realm.Realm

class ModifyGroupNameViewModel: ViewModel {
    constructor() : super() {
        realm = Realm.getInstance(ChatDB2.getConfiguration())
    }

    private var realm: Realm? = null

    fun getRoomBy(id: Int): Room? {
        return realm?.where(Room::class.java)
                ?.equalTo("id", id)
                ?.findFirst()
    }

    fun modifyRoomName(modifiedName: String, room: Room) {
        realm?.executeTransaction {
            room.name = modifiedName
            it.insertOrUpdate(room)
        }
    }

    override fun onCleared() {
        realm?.close()
        realm = null
        super.onCleared()
    }
}